#pragma once

#include<string>

extern "C"
{
#include "libavutil/avutil.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
}

#include <opencv2/opencv.hpp>
#include "ssiot/json.hpp"


class Record;
class YOLO;

class Rtsp 
{
public:
	Rtsp();
	~Rtsp();

	int		Open(nlohmann::json cfg);
	
	void	Close(void);

	void	Play(void);

	int 	ReadVidePkg(AVPacket* pkt);

private:
	int decode(AVCodecContext* avctx, AVFrame* frame, int* got_frame, AVPacket* pkt);

	int motion(cv::Mat& mat);

private:
	
	nlohmann::json		cfg_;

	AVPacket*			pkt_;
	AVFrame*			frame_;
	uint8_t*			frame_buf_;
	AVFrame*			frame_dec_;
	AVFormatContext*	fmt_ctx_;
	AVCodecContext*		avc_ctx_;
	AVCodecParameters*	avc_par_;
	SwsContext*			sws_ctx_;
	
	Record*				recode_;

	cv::Mat one, two, three, tempframe;
	cv::Mat ab, bc, difframe;
	int lastnozero;

	YOLO*				yolo_;
};
